import openpyxl
import openpyxl
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet


class DoFiles():
    #行为
    #文件：.txt .doc .docx  .xls  .xlsx   .ini .yam   .json
    def doTextRead(self,f,m,isUTF=False):
        # f=r"D:\work\softp\python\pro_test\datas\wk.txt"
        if isUTF:
               ff=open(f,m,encoding='utf-8')
        else:
            ff = open(f, m)
        return ff.read()
    def doTextWrite(self,f,m,s,isUTF=False):
        # f=r"D:\work\softp\python\pro_test\datas\wk.txt"
        # if isUTF:
        #        ff=open(f,mode=m,encoding='utf-8')
        # else:
        #     ff = open(f, mode=m)
        #
        # print(ff.write(s))
        # ff.close()
        #由于打开文件后，总是忘记关闭，很容易造成文件丢失，为了解决这个问题
        #采用with代码块的方式，一旦离开with代码块，文件自动关闭
        if isUTF:
               with open(f,mode=m,encoding='utf-8')as ff:
                   ff.write(s)
        else:
            with open(f, mode=m)as ff:
                ff.write(s)
    def doExeRead(self,f,stname,sr,er,sc,ec):
        wk:Workbook=openpyxl.load_workbook(f)
        st:Worksheet=wk[stname]
        # s=st.values
        s=st.iter_rows(sr,er,sc,ec,values_only=True)
        l=[i for i in s]
        # print(l)
        return l
if __name__ == '__main__':
    df=DoFiles()
    # f=r"D:\work\softp\python\pro_test\datas\my.xlsx"
    f=r"D:\work\softp\python\pro_test\datas\yunSuanData.xlsx"
    df.doExeRead(f)
    # f=r"D:\work\softp\python\pro_test\datas\wk.txt"
    # # df.doTextRead(f,'r',True)
    # f1=r"D:\work\softp\python\pro_test\datas\屏幕截图 2025-03-20 152622.png"
    # c=df.doTextRead(f1,'rb')
    # print(c)
    #
    #
    # df.doTextWrite(f,'w',"sss")
    # newFile = r"D:\work\softp\python\pro_test\datas\newFile.txt"
    # df.doTextWrite(newFile, 'a', "你好啊啊",True)
    # f2 = r"D:\work\softp\python\pro_test\datas\123.png"
    # df.doTextWrite(f2,'wb',c)